package com.rainwood.water_authority.ui.activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.zxing.android.QRCodeCaptureActivity;
import com.rainwood.water_authority.R;
import com.rainwood.water_authority.action.StatusAction;
import com.rainwood.water_authority.aop.SingleClick;
import com.rainwood.water_authority.base.MyBaseActivity;
import com.rainwood.water_authority.http.response.PanDianListBean;
import com.rainwood.water_authority.http.response.ShouLiaoListBean;
import com.rainwood.water_authority.ui.adapter.HistoryShouLiaoAdapter;
import com.rainwood.water_authority.ui.adapter.HistoryfaliaoAdapter;
import com.rainwood.water_authority.util.Constants;
import com.rainwood.water_authority.util.JsonUtils;
import com.rainwood.water_authority.util.LogUtils;
import com.rainwood.water_authority.util.SharePrenceHelper;
import com.rainwood.water_authority.widget.HintLayout;
import com.sxs.tools.base.BaseAdapter;
import com.sxs.tools.permission.OnPermission;
import com.sxs.tools.permission.Permission;
import com.sxs.tools.permission.XXPermissions;
import com.sxs.tools.titlebar.TitleBar;

import java.util.ArrayList;
import java.util.List;
/**
 * @author sxs
 * @date :2020/9/28
 * @desc: 发料
 */
public final class FaLiaoHomeAct  extends MyBaseActivity implements StatusAction, BaseAdapter.OnItemClickListener {
    public static void start(Context context) {
        Intent intent = new Intent(context, FaLiaoHomeAct.class);
        context.startActivity(intent);
    }

    private HistoryShouLiaoAdapter historyShouLiaoAdapter;
    private RecyclerView recycler_receiving;
    private HintLayout mHintLayout;
    private TitleBar title;
    private TextView tv_type;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_scan_treasury;
    }



    @Override
    protected void initView() {
        title=findViewById(R.id.title);
        title.setLeftTitle("发料");
        tv_type=findViewById(R.id.tv_type);
        tv_type.setText("扫单据条码发料");
        mHintLayout = findViewById(R.id.hl_state_page);
        recycler_receiving = findViewById(R.id.recycler_receiving);
        setOnClickListener(R.id.btn_scan_start);
        historyShouLiaoAdapter = new HistoryShouLiaoAdapter(this);
        historyShouLiaoAdapter.setOnItemClickListener(this);
        recycler_receiving.setAdapter(historyShouLiaoAdapter);

    }

    @Override
    public HintLayout getHintLayout() {
        return mHintLayout;
    }

    @Override
    protected void initData() {

    }
    @Override
    protected void onResume() {
        super.onResume();
        requestDatum();
    }
    private void requestDatum() {
        List<ShouLiaoListBean.DataBean.InfoBean> list = new ArrayList<>();
        String json = SharePrenceHelper.getStringData("faliao");
        if (json != null && !json.equals("")) {
            try {
                list = JsonUtils.jsonToArray(json, ShouLiaoListBean.DataBean.InfoBean.class);
                if (list.size() == 0) {
                    showEmpty(getDrawable(R.drawable.ic_empty_issue), "您还没有待发料的通知单");

                } else {
                    historyShouLiaoAdapter.clearData();
                    historyShouLiaoAdapter.setData(list);
                }
            } catch (Exception e) {

            }
        }
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_scan_start:
                XXPermissions.with(this)
                        .permission(Permission.CAMERA)
                        .permission(Permission.Group.STORAGE)
                        .request(new OnPermission() {
                            @Override
                            public void hasPermission(List<String> granted, boolean all) {
                                if (all) {
                                    //跳转扫码页面
                                    Intent intent = new Intent(getContext(), QRCodeCaptureActivity.class);
                                    intent.putExtra(QRCodeCaptureActivity.STATUS_BAR_COLOR, Color.parseColor("#99000000"));
                                    startActivityForResult(intent, Constants.REQUEST_SCAN_CODE);
                                }
                            }

                            @Override
                            public void noPermission(List<String> denied, boolean quick) {
                                if (quick) {
                                    toast(R.string.common_permission_fail);
                                    XXPermissions.startPermissionActivity(getActivity(), false);
                                } else {
                                    toast(R.string.common_permission_hint);
                                }
                            }
                        });
                break;
            default:
                break;
        }
    }

    //获取扫码结果
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_SCAN_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String result = null;
            if (bundle != null) {
                result = bundle.getString(QRCodeCaptureActivity.CODE_CONTENT);
                LogUtils.d(TAG, "-------- result-------- " + result);
            }
            if (TextUtils.isEmpty(result)) {
                toast("未识别到任何内容");
                return;
            }
            // result = "nrwk34z5yqsw";
            FaLiaoListAct.start(this, result);
        }
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        if (recyclerView.equals(recycler_receiving)) {
            FaLiaoListAct.start02(this, historyShouLiaoAdapter.getItem(position).getId(),
                    historyShouLiaoAdapter.getItem(position).getSource()
            );
        }
    }
}
